    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>jquery.js" ></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>jquery_std.js" ></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>common.js" ></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>popin.js" ></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>iutil.js"></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>idrag.js"></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>idrop.js"></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>isortables.js"></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>ajaxupload.js"></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>jq-tooltip.js"></script>
    <script type="text/javascript" src="<?=BASEURL.JS_DIR?>metadata.js" ></script>
    <script type="text/javascript">
    
    //data from server
    myData = {};
    myData.baseUrl  = "<?=site_url('');?>";
    myData.isLogged = true; 
    myData.galname = "<?=$galname?>";
    $.meta.setType("class");
    
    selectedVignette=null;
    
    function update_list()
    {
        $("#list").load(site_url('/admin/gallerymgr/listimgs/'+myData.galname),{ajax:"true"},assignVignettes);
    }
    
    function assignVignettes() {
     $('.vignette').click(function(){
          if (selectedVignette!=null) selectedVignette.removeClass("selected");
          $(this).addClass("selected");
          selectedVignette=$(this);
      });
      
      $('.vignette').dblclick(function(){
        if (selectedVignette!=null) selectedVignette.removeClass("selected");
          $(this).addClass("selected");
          selectedVignette=$(this);
          editInfos();
      });
      

    $('#list').Sortable(
			{
				accept : 		'vignette',
				helperclass : 	'sorthelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				opacity: 		0.8,
				revert:			true,
				floats:			true,
				tolerance:		'pointer',
				onStop: serialize
			});
			
			$('.vignette').Tooltip({
              track: true,
              delay: 0
            });
			
    }
    
    function serialize()
    {
       var sr="";
       //alert("sav");
       $('#list .vignette').each(function(i){
             sr+=i+'||';
             sr+=$(this).data().tags+'||';
             sr+=$(this).data().caption+'||';
             sr+=$(this).data().filename+'||'+"\n";
            });
            //alert(sr);
       $.post(site_url('/admin/gallerymgr/savechanges/'+myData.galname),{name:"name",template:"default",images_infos:sr}, 
       function(data){  } )    
    }
    
    function editInfos()
    {
             popIn(500,220,"#editinfos",'Edit image infos'); 
             $("#popInContent #tags").val(selectedVignette.data().tags);
             $("#popInContent #caption").val(selectedVignette.data().caption);
             $("#popInContent #filename").text(selectedVignette.data().filename);
             $("#popInContent img").attr("src",selectedVignette.data().url);
    }
    function updateInfos()
    {
             selectedVignette.data().tags=$("#popInContent #tags").val();
             selectedVignette.data().caption=$("#popInContent #caption").val();
             selectedVignette.attr('title',$("#popInContent #caption").val());
             selectedVignette.Tooltip({track: true,delay: 0});
             closePopIn();
             serialize();
    }

    $(document).ready(function() {
       
       
       //buttons & selects
       
       $("#btLogout").click(function(){ 
       });
       
       $('#btHelp').click( function(){
            popIn(400,500,site_url('/wiki/readpart/help'),'HELP');
       });
       
       $('#btCreateGal').click( function(){
               var nompage = normalize_str(prompt("nom de la nouvelle page"));
               if (nompage != null) 
                   window.location.href=site_url('/admin/wikimgr/edit/'+nompage);
                   
       });
       
       $('#btDeleteImg').click( function(){
              if (confirm ("are you sure ?"))
                  $.post(site_url('/admin/gallerymgr/delimg/')+myData.galname,{filename:selectedVignette.data().filename},update_list  );
       });
       
       $('#btAddImage').click( function(){
             popIn(280,150,"#uploadform",'Add an image'); 
       });       

       $('#btImageInfos').click( function(){
            editInfos();            
       }); 
       
       $('#btSaveInfos').click( function(){
            serialize();
       }); 
       
       update_list();

     
    });
    
        function testupload(_form, _uri) {
        $(".message",_form).html("Uploading...<br />please wait.");
        $.ajaxUpload({
            uploadform: _form,
            url: _uri,
            secureuri: false,
            dataType: 'json',
            success: function (msg) {
            if (msg.err!=undefined) {
                    $(".message",_form).html(msg.err);
                    } else {
                    closePopIn();
                    update_list();
                    }
                },
            error: function (msg, status, e) {
                 alert(e)
                }
            })
        }
        
    </script>
